package com.mlink.vs.service;

import com.mlink.vs.entity.PiccUser;
import com.mlink.vs.entity.PiccUserLocation;
import com.mlink.vs.entity.PiccUserLocationResult;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Generated;

import static com.mlink.vs.entity.PiccUserLocationNames.*;
import static org.seasar.extension.jdbc.operation.Operations.*;

/**
 * {@link PiccUserLocation}のサービスクラスです。
 * 
 */
@Generated(value = { "S2JDBC-Gen 2.4.47",
		"org.seasar.extension.jdbc.gen.internal.model.ServiceModelFactoryImpl" }, date = "2017-9-9 17:16:56")
public class PiccUserLocationService extends AbstractService<PiccUserLocation> {

	/**
	 * 識別子でエンティティを検索します。
	 * 
	 * @param id
	 *            識別子
	 * @return エンティティ
	 */
	public PiccUserLocation findById(String id) {
		return select().id(id).getSingleResult();
	}

	/**
	 * 識別子の昇順ですべてのエンティティを検索します。
	 * 
	 * @return エンティティのリスト
	 */
	public List<PiccUserLocation> findAllOrderById() {
		return select().orderBy(asc(id())).getResultList();
	}

	// 按时间段查询经纬度
	public List<PiccUserLocation> findAllbyId(String userId, String start, String end) {

		String sql = "select USER_ID, LON, LAT, UPDATE_TIME from PICC_USER_LOCATION  where   USER_ID=? AND UPDATE_TIME<= ? and UPDATE_TIME>= ?  ORDER BY UPDATE_TIME";

		return jdbcManager.selectBySql(PiccUserLocation.class, sql, userId, end, start).getResultList();

	}

	// 获取最新的一条数据
	public PiccUserLocationResult findOneById(String userId) {

//		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		String dateString = formatter.format(new Timestamp((new Date()).getTime()));
		String sql = "select PUL.USER_ID, PUL.LON, PUL.LAT,PUL.UPDATE_TIME , PU.USER_NAME  from PICC_USER_LOCATION AS PUL LEFT JOIN PICC_USER AS PU ON PUL.USER_ID=PU.USER_ID  where  PUL.USER_ID=?  ORDER BY UPDATE_TIME DESC limit 0,1";
//		String sql = "select PUL.USER_ID, PUL.LON, PUL.LAT,PUL UPDATE_TIME , PU.USER_NAME  from PICC_USER_LOCATION AS PUL LEFT JOIN PICC_USER AS PU ON PUL.USER_ID=PU.USER_ID  where  USER_ID=? and UPDATE_TIME<= ? ORDER BY UPDATE_TIME DESC limit 0,1";
		return jdbcManager.selectBySql(PiccUserLocationResult.class, sql, userId).getSingleResult();

	}

}